package com.altocloud.application;

import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.ParseException;

//import com.altoCloud.parsingUtility.ParseDataFilesLevel1;
//import com.altoCloud.testUtility.FileIterate;
import com.altocloud.database.DbUtil;
import com.altocloud.parse.Level1;

public class ApplicationLevel1 {

	File path;

	void iterateAllMesowestCSVFiles(String filePath) {
		File path = new File(filePath);
		setPath(path);
		int i = 0;
		File[] files = path.listFiles();
		while (i < files.length) {
			System.out.println("inside for loop");
			if (files[i].isFile()) { // this line weeds out other
										// directories/folders
				System.out.println("***" + files[i].toString()
						+ "***");
				Level1 p = new Level1();
				p.parseCsvFile(files[i].toString());
			}
			i++;
		}
	}

	void iterateMesowestCSVFiles(int size, String filePath) {
		File path = new File(filePath);
		setPath(path);
		int i = 0;
		File[] files = path.listFiles();

		while (i < files.length && i < size) {
			System.out.println("inside for loop");
			if (files[i].isFile()) { // this line weeds out other
										// directories/folders
				System.out.println("************" + files[i].toString()
						+ "**********");
				Level1 p = new Level1();
				p.parseCsvFile(files[i].toString());
			}
			i++;
		}

	}

	void iterateAllWeatherFiles(String filePath) {
		File path = new File(filePath);
		setPath(path);
		int i = 0;
		File[] files = path.listFiles();
		while (i < files.length) {
			System.out.println("inside for loop");
			if (files[i].isFile()) { // this line weeds out other
										// directories/folders
				System.out.println("************" + files[i].toString()
						+ "**********");
				Level1 p = new Level1();
				try {
					p.parseOutFile(files[i].toString());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			i++;
			System.out.println("NextFile");
		}
	}

	void iterateWeatherFiles(int size, String filePath) {
		File path = new File(filePath);
		setPath(path);
		int i = 0;
		File[] files = path.listFiles();
		while (i < files.length && i < size) {
			System.out.println("inside for loop");
			if (files[i].isFile()) { // this line weeds out other
										// directories/folders
				System.out.println("************" + files[i].toString()
						+ "**********");
				Level1 p = new Level1();
				try {
					p.parseOutFile(files[i].toString());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			i++;
		}
	}

	public File getPath() {
		return path;
	}

	public void setPath(File path) {
		this.path = path;
	}

	public static void main(String[] args) {

		String path = args[0];
		String fileName = args[1]; // CSV for CSV files and OUT for out files.
		int noOfFilesToInsert = 0;

		//int noOfFilesToInsert = 0;

		
		try {
			ApplicationLevel1 fi = new ApplicationLevel1();
			// To iterate over specified number of mesowest_csv.tbl files
			System.out.println("Execution Start");
			Long startTime = null;
			if (fileName.equalsIgnoreCase("csv")) {
		//		noOfFilesToInsert = Integer.parseInt(args[1]);
				startTime = System.currentTimeMillis();
				// To iterate over ALL mesowest_csv.tbl files
				fi.iterateAllMesowestCSVFiles(path);

			} else if (fileName.equalsIgnoreCase("out")) {
				noOfFilesToInsert = Integer.parseInt(args[2]);
				startTime = System.currentTimeMillis();
				// To iterate over specified number of mesowest.out file
				// fi.iterateWeatherFiles(91,"E:\\226data");
				fi.iterateWeatherFiles(noOfFilesToInsert, path);
			}

			Long endTime = System.currentTimeMillis();
			Long timeTaken = endTime - startTime;
			System.out.println("Execution End");
			System.out.println("Time Taken is : " + timeTaken + " ms");
		} catch (Exception e) {
			System.out.println("Please enter valid Parameters");
		}

	}
}
